登录 白背景

859. 亲密字符串

https://leetcode-cn.com/problems/buddy-strings/

  • 提交时间:2021-11-22 17:04:33
  • 执行用时:0 ms, 在所有 Go 提交中击败了100.00%的用户
  • 内存消耗:2.4 MB, 在所有 Go 提交中击败了58.57%的用户
  • 通过测试用例:34 / 34
func buddyStrings(s string, goal string) bool {
    sn := len(s)
    if sn < 2 {
        return false
    }
    if sn != len(goal) {
        return false
    }
    //字符串一致
    if s == goal {
        if sn > 26 {
            return true
        }
        for i := 0; i < sn-1; i++ {
            for o := i + 1; o < sn; o++ {
                if s[i] == s[o] {
                    return true
                }
            }
        }
    }
    //字符串不一致
    first, second := -1, -1
    for i := 0; i < sn; i++ {
        if s[i] != goal[i] {
            if first == -1 {
                first = i
            } else if second == -1 {
                second = i
            } else {
                return false
            }
        }
    }
    return first != -1 && second != -1 && s[first] == goal[second] && s[second] == goal[first]
}